<template>
  <div class="table-page">
    <GiTable
      row-key="id"
      title="报名管理"
      :data="dataList"
      :columns="columns"
      :loading="loading"
      :scroll="{ x: '100%', y: '100%', minWidth: 1000 }"
      :pagination="pagination"
      :disabled-tools="['size']"
      :disabled-column-keys="['name']"
      @refresh="search"
    >
      <template #custom-left>
        <a-button @click="reset">重置</a-button>
      </template>
      <template #custom-right>
        <a-button @click="onExport" type="primary">
          <template #icon>
            <icon-download />
          </template>
          导出比赛签到表
        </a-button>
      </template>
      <template #name="{ record }">
        <a-link @click="onDetail(record)">{{ record.name }}</a-link>
      </template>
      <template #action="{ record }">
        <a-space>
          <a-link
            v-permission="['sport:projectEntry:delete']"
            status="danger"
            :disabled="record.disabled"
            @click="onDelete(record)"
          >
            删除
          </a-link>
        </a-space>
      </template>
    </GiTable>
    <ProjectEntryDetailDrawer ref="ProjectEntryDetailDrawerRef" />
  </div>
</template>

<script setup lang="ts">
import ProjectEntryDetailDrawer from './ProjectEntryDetailDrawer.vue'
import { type ProjectEntryResp, type ProjectEntryQuery, deleteProjectEntry, exportProjectEntry, listProjectEntry } from '@/apis'
import type { TableInstanceColumns } from '@/components/GiTable/type'
import {useDownload, useTable} from '@/hooks'
import { isMobile } from '@/utils'
import has from '@/utils/has'

defineOptions({ name: 'ProjectEntry' })

const route = useRoute();
const projectId = route.query.id as string; // 获取传递的 id

const queryForm = reactive<ProjectEntryQuery>({
  projectId: projectId,
  userId: "",
  teamId: "",
  sort: ['createTime,desc']
})


const {
  tableData: dataList,
  loading,
  pagination,
  search,
  handleDelete
} = useTable((page) => listProjectEntry({ ...queryForm, ...page }), { immediate: true })

const columns: TableInstanceColumns[] = [
  { title: '项目名称', dataIndex: 'projectName', slotName: 'projectName' },
  { title: '参赛人员', dataIndex: 'userName', slotName: 'userName' },
  { title: '参赛团队', dataIndex: 'teamName', slotName: 'teamName' },
  {
    title: '操作',
    slotName: 'action',
    width: 130,
    align: 'center',
    fixed: !isMobile() ? 'right' : undefined,
    show: has.hasPermOr(['sport:projectEntry:update', 'sport:projectEntry:delete'])
  }
]

// 重置
const reset = () => {
  queryForm.projectId = ""
  queryForm.userId = ""
  queryForm.teamId = ""
  search()
}

// 删除
const onDelete = (item: ProjectEntryResp) => {
  return handleDelete(() => deleteProjectEntry(item.id), {
    content: `是否确定删除该条数据？`,
    showModal: true
  })
}

// 导出
const onExport = () => {
  useDownload(() => exportProjectEntry(queryForm))
}

const ProjectEntryDetailDrawerRef = ref<InstanceType<typeof ProjectEntryDetailDrawer>>()
// 详情
const onDetail = (item: ProjectEntryResp) => {
  ProjectEntryDetailDrawerRef.value?.onDetail(item.id)
}
</script>

<style lang="scss" scoped></style>
